package com.heaven.core.entity;

import com.heaven.constant.MessageConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document("message")
public class MessageEntity implements Serializable {
    @Id
    private String messageId;

    /** 消息来源的 userId; 0 代表是服务端的消息 */
    @Indexed(background = true)
    private String fromId;

    /** 消息接收者的 userId */
    @Indexed(background = true)
    private String toId;

    /** 消息状态码, 0就是正常否则异常 */
    private int code;

    /** 消息类型；-2, PONG，-1：PING，0：评论消息，1：弹幕消息，2：私聊消息 */
    private Integer type;

    /**消息读取状态 0: 未读；1: 已读 */
    private Integer status;

    /** 消息内容格式，0：文本, 1：图片，2：视频，3：其它类型格式文件*/
    private int contentFormat;

    /** 消息内容；文件类消息就为 {bucket}:{objectName}文件连接,
     * 评论消息或弹幕消息就存储 id
     * */
    private String msgContent;

    /** 消息的发送时间
     * {"fromId":"66f11d5c94daf611cb12e5f4","toId":"0","msgContent":"PING","sendTime":"2024-09-23T08:00:49.050Z","type":-1,"code":0,"status":1,"contentFormat":0}
     * */
    //@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date sendTime;  //发送时间

    /** 服务端向客户端私发文本消息的消息格式 */
    public MessageEntity(String msgContent) {
        this.fromId = "0";
        this.type = 2;
        this.contentFormat = 0;
        this.msgContent = msgContent;
        this.sendTime = new Date();
    }

    public MessageEntity(String msgContent, Integer type) {
        this.fromId = "0";
        this.type = type;
        this.contentFormat = 0;
        this.msgContent = msgContent;
        this.sendTime = new Date();
    }

    public boolean checkIsVaild() {
        boolean typeFlag = false;
        boolean formatFlag = false;
        Set<String> typeKeys = MessageConstants.MESSAGE_TYPE.keySet();
        Set<String> formatKeys = MessageConstants.MESSAGE_CONTENT_FORMAT.keySet();
        for (String type : typeKeys) {
            if(this.type == null){
                break;
            }
            if(this.type.equals(MessageConstants.MESSAGE_TYPE.get(type))){
                typeFlag = true;
                break;
            }
        }
        for (String format : formatKeys) {
            if(this.contentFormat == (MessageConstants.MESSAGE_CONTENT_FORMAT.get(format))){
                formatFlag = true;
                break;
            }
        }
        return this.fromId != null && !this.fromId.isEmpty()
                && this.toId != null && !this.toId.isEmpty()
                && typeFlag && formatFlag
                && this.status != null && (this.status == 0 || this.status == 1)
                && this.msgContent != null && !this.msgContent.isEmpty()
                && this.sendTime != null;
    }
}
